梦想不会自己发光,真正闪耀的是那个为梦狂奔的你。献给知行的孩子们!(Eric.He著)
队列是数据结构中经典的先进先出(FIFO, First In First Out)线性结构,就像日常生活中的排队买票——先排队的人先购票,后排队的人后购票。本教程基于链表实现的队列,从核心原理、代码结构到实战使用,全面讲解队列的原理与实现,功能包含入队、出队、获取队列头元素、获取队列尾元素等核心功能。
| 操作名称 | 功能描述 | 时间复杂度 |
|---|---|---|
| 入队 | 将指定元素添加到队尾,队列满时抛出异常或返回失败标识 | O(1) |
| 出队 | 从队头移除并返回元素,队列为空时抛出异常或返回失败标识 | O(1) |
| 判空 | 判断队列是否为空,返回布尔值(true = 空,false = 非空) | O(1) |
| 判满 | 判断固定容量队列是否已满,返回布尔值(true = 满,false = 未满)(链式队列无需此操作) | O(1) |
| 获取队头元素 | 查看队头元素的值,不执行移除操作,队列为空时抛出异常或返回无效值 | O(1) |
| 获取队尾元素 | 查看队尾元素的值,不执行移除操作,队列为空时抛出异常或返回无效值 | O(1) |
| 获取队列大小 | 查看队列元素数量,队列为空时返回0 | O(1) |
队列可以采用顺序存储和链式存储两种形式:
每一个节点有二个域,即data数据域、next下一个节点地址。
提供的代码是模板化的队列实现(支持任意类型),采用C++结构体封装,分为头文件(声明)和源文件(实现)两部分,核心结构如下:
| 模块 | 作用 | 关键结构/函数 |
|---|---|---|
| 节点结构体 | 存储数据、下一个节点地址 | QueueNode<T>(模板泛型) |
| 队列结构体 | 封装所有操作 | LinkedQueue<T>(模板类) |
template <typename T>
struct QueueNode
{
T data; //队列节点数据
QueueNode* next; //下一个队列节点指针
};
template <typename T>
struct LinkedQueue{
//------------------------------------------------------------------------------------------------------
// 私有成员 ||
// 注:私有成员只能在该结构体内部访问调用,外部通过该结构体定义(实例化)的变量(对象)不能访问调用) ||
//------------------------------------------------------------------------------------------------------
private:
//---------------声明私有成员变量---------------
QueueNode<T> *head = nullptr; // 队列头指针
QueueNode<T> *tail = nullptr; // 队列尾指针
int qty = 0; // 用于跟踪队列中的元素数量
//---------------声明私有成员函数---------------
//------------------------------------------------------------------------------------------------------
// 公共成员 ||
// 注:公共成员能在该结构体内部访问调用,外部通过该结构体定义(实例化)的变量(对象)也能访问调用) ||
//------------------------------------------------------------------------------------------------------
public:
//---------------声明公共成员函数---------------
// 构造函数:外部通过该结构体定义(实例化)变量(对象)时,自动执行该函数(主要用于初始化成员变量的值)
LinkedQueue() {
qty = 0;
}
int isEmpty(); // 判断队列是否为空
int isFull(); // 判断队列是否已满
T getHead(); // 获取队列头元素(不弹出)
T getTail(); // 获取队列尾元素(不弹出)
void enQueue(const T& value); // 入队操作(在队列尾入队)
T deQueue(); // 出队操作(返回队列头元素)
int getSize(); // 获取队列大小
void print(); // 打印队列数据
void destroyQueue(); // 手动销毁整个栈(防止内存泄漏)
// 析构函数:自动销毁栈,避免内存泄漏
~LinkedQueue() {
destroyQueue();
}
};
int、float、自定义类型等任意类型(需重载比较运算符);template <typename T>
T LinkedQueue<T>::getHead()
{
if (isEmpty()) {
return T();
}
return head->data;
}
template <typename T>
T LinkedQueue<T>::getTail()
{
if (isEmpty()) {
return T();;
}
return tail->data;
}
template <typename T>
void LinkedQueue<T>::enQueue(const T& value)
{
QueueNode<T> *p = new QueueNode<T>;
p->data = value;
p->next = nullptr;
if(isEmpty())
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
qty++;
}
template <typename T>
T LinkedQueue<T>::deQueue()
{
T value;
if(isEmpty())
{
printf("队列为空,无法出队。\n");
return T();;
}
QueueNode<T> *p = head;
head = p->next;
value = p->data;
delete(p);
qty--;
return value;
}
#include "LinkedQueue.h"
#include <iostream>
int main() {
LinkedQueue<int> queues;
// 1. 入队测试
printf("\n=== 入队测试 ===\n");
queues.enQueue(1);
queues.enQueue(2);
queues.enQueue(3);
queues.enQueue(4);
queues.enQueue(5);
printf("入队1、2、3、4、5后:\n");
queues.print();
// 2. 出队测试
printf("\n=== 出队测试 ===\n");
printf("出队3次:");
printf("%d ",queues.deQueue());
printf("%d ",queues.deQueue());
printf("%d \n",queues.deQueue());
printf("出队后");
queues.print();
return 0;
}
=== 入队测试 ===
入队1、2、3、4、5后:
队列内容:1 2 3 4 5
=== 出队测试 ===
出队3次:1 2 3
出队后队列内容:4 5
===== 队列内存已全部释放 =====
#ifndef ENTITYS_H_INCLUDED
#define ENTITYS_H_INCLUDED
//************************************************************************************************************************************************************************
// 自定义类型
//************************************************************************************************************************************************************************
//========================================================================================================================================================================
// 学生结构体(Student)
//========================================================================================================================================================================
struct Student {
int id;// 学号
std::string name;// 姓名
std::string dob;// 出生日期
std::string sex;// 性别
std::string gender;// 民族
std::string address;// 地址
float score;// 入学总分
std::string school;// 学校
std::string team;// 班级
std::string status;// 状态
bool operator<(const Student& other) const { return id < other.id; }
bool operator>(const Student& other) const { return id > other.id; }
bool operator==(const Student& other) const { return id == other.id; }
bool operator!=(const Student& other) const { return id != other.id; }
friend std::ostream& operator<<(std::ostream& os, const Student& s) {
os << "[" << s.id<< ", " << s.name << ", " << s.dob << ", " << s.sex << ", " << s.gender << ", " << s.address << ", " << s.score<< ", " << s.school<< ", " << s.team<< ", " << s.status << "]";
return os;
}
};
//========================================================================================================================================================================
//
// 学生索引结构体(Student)
//
//========================================================================================================================================================================
struct StudentIndex {
int id;// 学号
int row;// 行号
bool operator<(const StudentIndex& other) const { return id < other.id; }
bool operator>(const StudentIndex& other) const { return id > other.id; }
bool operator==(const StudentIndex& other) const { return id == other.id; }
bool operator!=(const StudentIndex& other) const { return id != other.id; }
friend std::ostream& operator<<(std::ostream& os, const StudentIndex& i) {
os << "[" << i.id << ", " << i.row<< "]";
return os;
}
};
//========================================================================================================================================================================
// 迷宫坐标结构体(Pos)
//========================================================================================================================================================================
struct Pos{
int x; //x坐标
int y; //y坐标
int step; //步数
};
//========================================================================================================================================================================
// 打印任务结构体(PrintTask)
//========================================================================================================================================================================
struct PrintTask{
int taskId; // 任务ID
char content[50]; // 打印内容
};
#endif // ENTITYS_H_INCLUDED
#ifndef LINKEDQUEUE_H_INCLUDED
#define LINKEDQUEUE_H_INCLUDED
#include "Entitys.h"
//************************************************************************************************************************************************************************
//
// 类名:链表队列(LinkedQueue)
//
// 概述:一个可复用的链表队列,存储结构采用链表实现,可存储字符或整数。
//
// 说明:采用链表存储结构,队列的容量没限制,入队出队时间复杂度为O(1)
//
//************************************************************************************************************************************************************************
//========================================================================================================================================================================
//
// 队列节点结构体(QueueNode)
//
//========================================================================================================================================================================
template <typename T>
struct QueueNode
{
T data; //队列节点数据
QueueNode* next; //下一个队列节点指针
};
//========================================================================================================================================================================
//
// 链表队列结构体(LinkedQueue)
//
//========================================================================================================================================================================
template <typename T>
struct LinkedQueue{
//------------------------------------------------------------------------------------------------------
// 私有成员 ||
// 注:私有成员只能在该结构体内部访问调用,外部通过该结构体定义(实例化)的变量(对象)不能访问调用) ||
//------------------------------------------------------------------------------------------------------
private:
//---------------声明私有成员变量---------------
QueueNode<T> *head = nullptr; // 队列头指针
QueueNode<T> *tail = nullptr; // 队列尾指针
int qty = 0; // 用于跟踪队列中的元素数量
//---------------声明私有成员函数---------------
//------------------------------------------------------------------------------------------------------
// 公共成员 ||
// 注:公共成员能在该结构体内部访问调用,外部通过该结构体定义(实例化)的变量(对象)也能访问调用) ||
//------------------------------------------------------------------------------------------------------
public:
//---------------声明公共成员函数---------------
// 构造函数:外部通过该结构体定义(实例化)变量(对象)时,自动执行该函数(主要用于初始化成员变量的值)
LinkedQueue() {
qty = 0;
}
int isEmpty(); // 判断队列是否为空
int isFull(); // 判断队列是否已满
T getHead(); // 获取队列头元素(不弹出)
T getTail(); // 获取队列尾元素(不弹出)
void enQueue(const T& value); // 入队操作(在队列尾入队)
T deQueue(); // 出队操作(返回队列头元素)
int getSize(); // 获取队列大小
void print(); // 打印队列数据
void destroyQueue(); // 手动销毁整个栈(防止内存泄漏)
// 析构函数:自动销毁栈,避免内存泄漏
~LinkedQueue() {
destroyQueue();
}
};
#endif // LINKEDQUEUE_H_INCLUDED
#include <iostream>
#include"LinkedQueue.h"
//---------------实现公共成员函数---------------
// 判断队列是否为空
template <typename T>
int LinkedQueue<T>::isEmpty()
{
return qty == 0;
}
// 判断队列是否已满
template <typename T>
int LinkedQueue<T>::isFull()
{
return -1;
}
// 获取队列头元素(不弹出)
template <typename T>
T LinkedQueue<T>::getHead()
{
if (isEmpty()) {
return T();
}
return head->data;
}
// 获取队列尾元素(不弹出)
template <typename T>
T LinkedQueue<T>::getTail()
{
if (isEmpty()) {
return T();;
}
return tail->data;
}
// 入队操作(在队列尾入队)
template <typename T>
void LinkedQueue<T>::enQueue(const T& value)
{
QueueNode<T> *p = new QueueNode<T>;
p->data = value;
p->next = nullptr;
if(isEmpty())
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
qty++;
}
// 出队操作(返回队列头元素)
template <typename T>
T LinkedQueue<T>::deQueue()
{
T value;
if(isEmpty())
{
printf("队列为空,无法出队。\n");
return T();;
}
QueueNode<T> *p = head;
head = p->next;
value = p->data;
delete(p);
qty--;
return value;
}
// 获取队列大小
template template <typename T>
int LinkedQueue<T>::getSize()
{
return qty;
}
// 打印队列数据
template <typename T>
void LinkedQueue<T>::print()
{
printf("队列内容:");
QueueNode<T> *p = head;
if(p == nullptr)
{
printf("队列为空!无法遍历。\n");
return;
}
while(p != nullptr)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
// 销毁整个栈(防止内存泄漏)
template <typename T>
void LinkedQueue<T>::destroyQueue()
{
while(head != nullptr)
{
QueueNode<T> *p = head;
head = p->next;
delete(p);
qty--;
}
printf("\n===== 队列内存已全部释放 =====\n");
}
// 显式实例化常用类型(避免链接错误,可选)
template class LinkedQueue<int>;
template class LinkedQueue<char>;
template class LinkedQueue<float>;
template class LinkedQueue<std::string>;
template class LinkedQueue<Student>; // 新增:显式实例化Student类型
template class LinkedQueue<Pos>; // 新增:显式实例化Pos类型
template class LinkedQueue<PrintTask>; // 新增:显式实例化PrintTask类型
#include "LinkedQueue.h"
#include <iostream>
#include <string>
int main() {
LinkedQueue<int> queues;
// 1. 入队测试
printf("\n=== 入队测试 ===\n");
queues.enQueue(1);
queues.enQueue(2);
queues.enQueue(3);
queues.enQueue(4);
queues.enQueue(5);
printf("入队1、2、3、4、5后:\n");
queues.print();
// 2. 出队测试
printf("\n=== 出队测试 ===\n");
printf("出队3次:");
printf("%d ",queues.deQueue());
printf("%d ",queues.deQueue());
printf("%d \n",queues.deQueue());
printf("出队后");
queues.print();
return 0;
}
=== 入队测试 ===
入队1、2、3、4、5后:
队列内容:1 2 3 4 5
=== 出队测试 ===
出队3次:1 2 3
出队后队列内容:4 5
===== 队列内存已全部释放 =====
模板类型约束:
</>/==运算符(内置类型如int/string默认支持,自定义类型需重载);<<运算符(打印输出时使用)。显式实例化:代码末尾的template class LinkedQueue<int>;等显式实例化,避免链接错误,新增类型需补充显式实例化。
本教程通过代码解析和实战示例,覆盖了队列的核心原理、关键操作实现和实际使用场景。队列作为基础数据结构,在缓存机制、任务调度、消息队列等场景中有着广泛应用,掌握链表队列的实现,是深入学习更复杂数据结构(如优先级队列)的重要基础。